<?php


include 'D:\wangkai\汇付宝phpAPI\客户中心RSA2 PHP\Util.php';
use function Util\ASCIIAZ;
use function Util\RSA2sign;
use function Util\DeleteValue;
use function Util\PostJson;
use function Util\verifysign;
/*
 入驻申请
*/

//入驻申请业务参数 文档里必传字段必须传
$Oldbiz_content = array(
    "if_sign" => '1',
    "customer_type" => '1',                       //1=企业 2=个体工商户 3=小微
    "register_type" => 'HEEPAY_MERCH',            //账户类型（默认收单）HEEPAY_MERCH=收单商户；HEEPAY_BALANCE=钱包账户
    "customer_name" => '小二黑科技有限公司（测试)',        //客户名称
    "customer_short_name" => '小锤',               //客户简称
    "customer_phone" => '010-66633558',            //客服电话
    "licence_code" => '98313312MA7HB6C89P',       //营业执照编码
    "licence_valid_type" => '1',                   //营业执照有效期类型  1=定期,2=长期   当 legal_id_card_valid_type =2时，legal_id_card_valid_end 为空
    "licence_valid_begin" => '2021-05-21',         //营业执照生效日期
    "licence_valid_end" => '2033-05-21',           //营业执照失效日期
    "licence_region_code" => '110108',             //营业执照区划编码
    "licence_address" => '北京市海淀区中关村',       //营业执照注册地址
    "business_category_code" => '1035',            //经营类目编码
    "legal_name" => '张三',                        //法人姓名
    "legal_id_card" => '610321192303032321',       //法人证件号
    "legal_id_card_valid_type" => '1',             //法人有效期类型  1=定期,2=长期    当 legal_id_card_valid_type=2时，legal_id_card_valid_end为空
    "legal_id_card_valid_begin" => '2023-10-26',   //法人证件生效日期
    "legal_id_card_valid_end" => '2033-10-26',      //法人证件失效日期
    "legal_mobile" => '18312344321',                //法人手机号
    "contact_name" => '李四',                       //联系人名称
    "contact_id_card" => '610321199303203921',      //联系人证件号
    "contact_id_card_valid_type" => '1',           //联系人证件有效期类型 1=定期,2=长期     当 contact_id_card_valid_type=2时，contact_id_card_valid_end为空
    "contact_id_card_valid_begin" => '2021-05-21',  //联系人证件生效日期
    "contact_id_card_valid_end" => '2041-05-21',    //联系人证件失效日期
    "contact_mobile" => '13943211234',              //联系人手机号
    "contact_email" => '123336663@qq.com',      //联系人邮箱/创建商户登录账号
    "bank_account_type" => '1',                     //结算银行账户类型
    "bank_account_name" => '小二黑科技有限公司（测试)',      //结算银行账户名称
    "bank_card_no" => '61021234123',                //结算银行卡号
    "bank_name" => '建设银行',                       //结算开户行名称
    "bank_region_code" => '110100',                  //结算银行所属区划编码
    "withdraw_type" => '1',                          //提现类型 1=手动提现,2=自动提现
    "licence_img" => '17267363880630af4aa4622356867989',//营业执照图片路径
    "company_build_img" => '17267363880630af4aa4622356867989',//公司大楼图片路径
    "company_front_img" => '17267363880630af4aa4622356867989',//公司前台图片路径
    "shop_env_img" => '17267363880630af4aa4622356867989',//店内场景照图片路径
    "reg_certificate_img" => '17267363880630af4aa4622356867989',//开户许可证图片路径
    "legal_id_card_front_img" => '17267363880630af4aa4622356867989',//法人身份证人像面图片路径
    "legal_id_card_back_img" => '17267363880630af4aa4622356867989',//法人身份证国徽面图片路径
    "contact_id_card_front_img" => '17267363880630af4aa4622356867989',// 业务员身份证人像面图片路径
    "contact_id_card_back_img" => '17267363880630af4aa4622356867989',//业务员身份证国徽面图片路径
    "beneficiary_is_legal" => '0',//受益人是否是法人， 默认1，如果是1，法人的控股比例是100% 。收单户必填，钱包户选填
);


/*
公共请求参数
*/
$app_id='hy20240426110700440847';
$charset='utf-8';
$biz_content=json_encode($Oldbiz_content,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
$method='customer.entry.apply';
$format='json';
$sign_type='RSA2';
$version='1.0';
$notify_url='http://www.baidu.com';


/*
生成待签名串方法
签名通用步骤第一步，设所有发送或者接收到的数据为集合M，将集合M内非空参数值的参数按照参数名ASCII码从小到大排序（字典序），使用URL键值对的格式（即key1=value1&key2=value2…）拼接成字符串stringA 特别注意以下重要规则：
1.参数名ASCII码从小到大排序（字典序）；
2.如果参数的值为空不参与签名；
3.参数名区分大小写；第二步，使用商户RSA2私钥对待签名字符串stringA进行签名，得到签名sign
*/
$arrayData = array(
    'app_id' => $app_id,
    'biz_content' => $biz_content,
    'charset' => $charset,
    'method' => $method,
    'format' => $format,
    'sign_type' => $sign_type,
    'version' => $version,
    'notify_url'=>$notify_url
);


$signatureContent = ASCIIAZ($arrayData);
echo "签名前参数：".$signatureContent  .PHP_EOL;


$sign = RSA2sign($signatureContent);


// 构建请求的JSON数据
$request_data = array(
    "charset" => $charset,
    "biz_content" => $biz_content,
    "method" => $method,
    "format" => $format,
    "sign" => $sign,
    "sign_type" => $sign_type,
    "version" => $version,
    "app_id" => $app_id,
    'notify_url'=>$notify_url
);


// 将数组转换为JSON字符串
$jsonString = json_encode($request_data,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
$jsonData = DeleteValue($jsonString);


//输出请求数据
echo "请求json".$jsonData .PHP_EOL;


$Posturl = "https://openapi.heepay.com/customer-api/gateway";

$Response = PostJson($Posturl,$jsonData);

//获取响应参数
$response = json_decode($Response, true);
$bizContentJson = json_encode(array('apply_no' => $response['biz_content']['apply_no']), JSON_UNESCAPED_UNICODE + JSON_UNESCAPED_SLASHES);
$queryString = 'biz_content=' . $bizContentJson . '&code=' . urlencode($response['code']) . '&msg=' . urlencode($response['msg']) .'&trace_id=' . urlencode($response['trace_id']);

//同步返回验签
if($response['code']===10000){

    echo "响应结果------->" .$Response . PHP_EOL;
    echo "验签串------>" .$queryString .PHP_EOL;
    verifysign($queryString,$response['sign']);
}
else
{
    echo $Response;
}


?>